﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using VMDS.Business.Entities;
using VMDS.Business.Processes;
using VMDS.Settings;

namespace VMDS.Services.ClientService
{
    class Cache
    {
        private ClientConfiguration Cfg;
        public List<VM> OnlineVMs
        {
            get { return XmlTool.Load(Cfg.VmList, typeof(List<VM>)); }
            set { XmlTool.Save(Cfg.VmList, value); }
        }
        public List<VM> InstalledVMs
        {
            get
            {
                if(!File.Exists(Cfg.InstalledVmList)) InstalledVMs=new List<VM>();
                return XmlTool.Load(Cfg.InstalledVmList, typeof(List<VM>));
            }
            set { XmlTool.Save(Cfg.InstalledVmList, value); }
        }
        public List<OS> OnlineOSs
        {
            get
            {
                if (!File.Exists(Cfg.OsList)) OnlineOSs=new List<OS>();
                return XmlTool.Load(Cfg.OsList, typeof (List<OS>));
            }
            set { XmlTool.Save(Cfg.OsList, value); }
        }
        public List<OS> InstalledOSs
        {
            get
            {
                if (!File.Exists(Cfg.InstalledOsList)) InstalledOSs=new List<OS>();
                return XmlTool.Load(Cfg.InstalledOsList, typeof(List<OS>));
            }
            set { XmlTool.Save(Cfg.InstalledOsList, value); }
        }
        public List<Task> TaskList
        {
            get
            {
                if (!File.Exists(Cfg.TaskList)) TaskList = new List<Task>();
                return XmlTool.Load(Cfg.TaskList, typeof(List<Task>));
            }
            set { XmlTool.Save(Cfg.TaskList, value); }
        }
        public void TestFill()
        {
            OnlineVMs = new List<VM>()
                      {
                          new VM(){Id=1, Description="virtual mashine with id 1", Name="One"},
                          new VM(){Id=2, Description="virtual mashine with id 2", Name="two"},
                          new VM(){Id=3, Description="virtual mashine with id=3", Name="three"}

                      };
            InstalledVMs = new List<VM>()
                               {
                                   OnlineVMs.SingleOrDefault(x => x.Id == 2)
                               };
        }


        public Cache(ClientConfiguration cfg)
        {
            Cfg = cfg;
        }
    }
}
